package com.chance.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chance.model.entity.*;
import com.chance.model.enums.ItemType;
import com.chance.web.admin.mapper.*;
import com.chance.web.admin.service.*;
import com.chance.web.admin.vo.attr.AttrValueVo;
import com.chance.web.admin.vo.graph.GraphVo;
import com.chance.web.admin.vo.room.RoomDetailVo;
import com.chance.web.admin.vo.room.RoomItemVo;
import com.chance.web.admin.vo.room.RoomQueryVo;
import com.chance.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HUANG
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2024-10-30 12:33:34
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Autowired
    private RoomLabelMapper roomLabelMapper;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;


    /**
     * 获取分页数据
     * @param page 分页对象
     * @param queryVo 查询参数
     * @return 返回分页数据
     */
    @Override
    public IPage<RoomItemVo> selectPageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.selectPageItem(page, queryVo);
    }


    /**
     * 获取房间详情信息
     * @param id 房间id
     * @return 返回房间详情信息
     */
    @Override
    public RoomDetailVo getRoomById(Long id) {
        // 查询房间信息
        RoomInfo roomInfo = super.getById(id);
//        // 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        // 查询图片列表
        List<GraphVo> graphVos = graphInfoMapper.selectItemTypeAndId(ItemType.ROOM, id);
        // 查询属性列表
        List<AttrValueVo> attrValueVos = attrValueMapper.selectItemTypeAndId(id);
        // 查询配套列表
        List<FacilityInfo> facilityInfos = roomFacilityMapper.selectItemTypeAndId(id);
        // 查询标签列表
        List<LabelInfo> labelInfos = roomLabelMapper.selectItemTypeAndId(id);
        //  查询支付方式列表
        List<PaymentType> paymentTypes = roomPaymentTypeMapper.selectItemTypeAndId(id);
//        // 查询可选租期列表
        List<LeaseTerm> leaseTerms = roomLeaseTermMapper.selectItemTypeAndId(id);
        // 组装数据
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setAttrValueVoList(attrValueVos);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setLeaseTermList(leaseTerms);
        return roomDetailVo;
    }


    /**
     * 房间保存或更新
     * @param roomSubmitVo 房间保存或更新信息
     */
    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        Boolean isUpdate = roomSubmitVo.getId() != null;

        if (isUpdate) {
            Long roomId = roomSubmitVo.getId();
            removeRoomItem(roomId);
        }

        super.saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();
        // 插入图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            graphInfoService.saveBathById(graphVoList, ItemType.ROOM, roomId);
        }
        // 插入属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (attrValueIds != null) {
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomId);
                roomAttrValue.setAttrValueId(attrValueId);
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }

        //  插入配套列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (facilityInfoIds != null) {
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setRoomId(roomId);
                roomFacility.setFacilityId(facilityInfoId);
                roomFacilities.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }

        // 插入标签列表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (labelInfoIds != null) {
            ArrayList<RoomLabel> roomLabels = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setRoomId(roomId);
                roomLabel.setLabelId(labelInfoId);
                roomLabels.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabels);
        }

        // 插入支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (paymentTypeIds != null) {
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomId).paymentTypeId(paymentTypeId).build();
                roomPaymentTypes.add(roomPaymentType);
            }
        }

        // 可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (leaseTermIds != null) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomId).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
        }
    }


    /**
     * 删除房间
     * @param id 房间id
     */
    @Override
    public void removeRoomById(Long id) {
        this.removeRoomItem(id);
        roomInfoMapper.deleteById(id);
    }


    /**
     * 按公寓id查找房间
     * @param id 公寓id
     * @return 房间列表
     */
    @Override
    public List<RoomInfo> selectByApartmentId(Long id) {
        List<RoomInfo> roomInfos = roomInfoMapper.selectList(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        return roomInfos;
    }


    /**
     * 删除房间item关联信息
     * @param id 房间id
     */
    public void removeRoomItem(Long id) {
        // 删除图片
        graphInfoMapper.delete(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId, id));
        // 删除属性列表
        roomAttrValueMapper.delete(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        // 删除配套
        roomFacilityMapper.delete(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        // 删除标签
        roomLabelMapper.delete(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        // 删除支付方式
        roomPaymentTypeMapper.delete(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
        // 删除可选租期列表
        roomLeaseTermMapper.delete(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
    }
}




